package com.hd123.rumba.gwt.widget.client;

import java.util.List;
import java.util.Vector;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

public class BillHeaderFlexTable extends FlexTable {

  private int columnCount = 2;
  private List fields;

  public BillHeaderFlexTable() {
    super();
    setStyleName("jdgwt-BillHeader");
    setCellPadding(1);
    setCellSpacing(1);
    setWidth("100%");

    fields = new Vector();
  }

  public int getColumnCount() {
    return columnCount;
  }

  public void setColumnCount(int columnCount) {
    this.columnCount = columnCount;
  }

  public List getFields() {
    return fields;
  }

  public void addField(Widget widget) {
    fields.add(widget);
  }

  public void removeField(Widget widget) {
    fields.remove(widget);
  }

  /**
   * 根据fields内容重新构造，隐藏的field不占用位置。
   * 
   */
  public void rebuild() {
    int row = 0, col = 0;

    for (int i = 0; i < fields.size(); i++) {
      Widget widget = (Widget) fields.get(i);

      if (!widget.isVisible())
        continue;

      setWidget(row, col, widget);

      col++;
      if (col == columnCount) {
        row++;
        col = 0;
      }
    }
  }

}
